package com.zhengbing.tj.design.async;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;

/**
 * 异步执行演示主类
 * Spring Boot异步执行功能完整示例
 * 
 * 功能特点：
 * 1. 配置异步执行线程池
 * 2. 多种异步执行模式演示
 * 3. 异常处理和监控
 * 4. RESTful接口测试
 * 
 * @author zhengbing
 * @since 2024-08-21
 */
@SpringBootApplication
@ComponentScan(basePackages = {"com.zhengbing.tj"})
public class AsyncDemoMain {

    private static final Logger logger = LoggerFactory.getLogger(AsyncDemoMain.class);

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(AsyncDemoMain.class, args);
        
        logger.info("=========================================");
        logger.info("Spring Boot异步执行示例已启动");
        logger.info("=========================================");
        logger.info("可用的测试接口：");
        logger.info("1. GET  /design/async/simple?message=测试消息");
        logger.info("2. GET  /design/async/calculate?number=5");
        logger.info("3. POST /design/async/email?email=test@example.com&content=测试内容");
        logger.info("4. POST /design/async/batch?items=任务1,任务2,任务3");
        logger.info("5. GET  /design/async/risky?shouldFail=true");
        logger.info("6. GET  /design/async/combined?numbers=1,2,3,4,5");
        logger.info("=========================================");
        
        // 演示直接调用异步服务
        AsyncService asyncService = context.getBean(AsyncService.class);
        
        // 演示简单的异步调用
        logger.info("启动演示：执行简单异步任务");
        asyncService.simpleAsyncTask("系统启动测试");
        
        // 演示带返回值的异步调用
        logger.info("启动演示：执行计算任务");
        asyncService.calculateAsync(10)
            .whenComplete((result, throwable) -> {
                if (throwable != null) {
                    logger.error("演示计算失败", throwable);
                } else {
                    logger.info("演示计算结果: 10² = {}", result);
                }
            });
    }
}